package com.xk857.simpleeffect.db;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.xk857.simpleeffect.domain.entity.Customer;

import cn.hutool.core.util.StrUtil;

public class PersonDataBase {

    /**
     * 二维码扫描key，生成二维码
     */
    public static final String ADD_FRIEND_KEY = "add:friend:code:";

    /**
     * 获取个人信息
     */
    public static Customer getPersonInfo(Context context) {
        Gson gson = new Gson();
        SharedPreferences preferences = context.getSharedPreferences("userInfo", MODE_PRIVATE);
        String customerStr = preferences.getString("customer", "");
        if (StrUtil.isBlank(customerStr)) {
            return null;
        }
        return gson.fromJson(customerStr, Customer.class);
    }

    public static void removePersonInfo(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("userInfo", MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.remove("token");
        edit.remove("customer");
        edit.apply();//提交
    }

    /**
     * 获取个人信息
     */
    public static void setPersonInfo(Context context, Customer customer) {
        Gson gson = new Gson();
        SharedPreferences preferences = context.getSharedPreferences("userInfo", MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString("token", customer.getToken());
        edit.putString("customer", gson.toJson(customer));
        edit.apply();//提交
    }

    /**
     * 获取个人信息
     */
    public static String getToken(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("userInfo", MODE_PRIVATE);
        return preferences.getString("token", "");
    }

    /**
     * 是否播放音频
     */
    public static Boolean isPlay(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("setting", MODE_PRIVATE);
        return preferences.getBoolean("play", true);
    }

    /**
     * 是否震动
     */
    public static Boolean isVibrate(Context context) {
        SharedPreferences preferences = context.getSharedPreferences("setting", MODE_PRIVATE);
        return preferences.getBoolean("vibrate", false);
    }

    /**
     * 修改是否播放音频
     */
    public static void setPlay(Context context, Boolean flag) {
        SharedPreferences preferences = context.getSharedPreferences("setting", MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean("play", flag);
        edit.apply();
    }

    /**
     * 修改是否震动
     */
    public static void setVibrate(Context context, Boolean flag) {
        SharedPreferences preferences = context.getSharedPreferences("setting", MODE_PRIVATE);
        SharedPreferences.Editor edit = preferences.edit();
        edit.putBoolean("vibrate", flag);
        edit.apply();//提交
    }
}
